<template>
  <div id="CartBottomBar" class="bottom-bar">
    <div class="check-content">
      <check-button  class="check-button" :is-chenked="isSelectAll" 
      @click.native="checkClik"></check-button>
      <span>全选</span>
    </div>   
    <div class="price">合计:{{totalPrice}}</div>
    <div class="calculate" @click="calcClick">
      去计算({{checkLength}})
    </div>
  </div>
</template>

<script>
import CheckButton from 'components/content/checkButton/CheckButton.vue'
import { mapGetters } from "vuex"
export default {
  name: "CartBottomBar",
  components: {
    CheckButton
  },
  computed: { //计算属性
    ...mapGetters( ["cartList"] ),
    totalPrice() {
      return this.cartList.filter(item => {
        return item.checked
      }).reduce((preValue, item) => {
        return preValue + item.price * item.count
      },0).toFixed(2)
    },
    checkLength() {
      return this.cartList.filter(item => 
        item.checked
      ).length
    },
    isSelectAll() {
      //return !(this.cartList.filter(item => !item.checked).length)
      if (this.cartList.length === 0) {
        return false
      }
      else {
        return !this.cartList.find(item => !item.checked)
      }      
    }
  },
  methods: {//方法
    checkClik() {
      //如果原来都是选中，点击以后全部不选中
      if (this.isSelectAll) { //全部选中
        this.cartList.forEach(item => item.checked = false)
      }
      else { //部分选中或全部不选中
        this.cartList.forEach(item => item.checked = true)
      }
    },
    calcClick() {
      if (!this.isSelectAll) {
        this.$toast.show('请选择购买的商品')
      }
    }
  }
}
</script>

<style scoped>
.bottom-bar {
  height: 40px;
  background-color: #eee;
  position: relative;
  line-height: 40px;
  display: flex;
  font-size: 14px;
}
.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
}
.price {
  margin-left: 20px;
  flex: 1;
}
.calculate {
  width: 90px;
  background: red;
  color: #fff;
  text-align: center;
}
</style>
